package com.banciyuan.bcywebview.biz.circles.discover;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.fragment.BaseFragment;
import com.banciyuan.bcywebview.biz.circles.fragment.list.CircleProductionFragment;
import com.banciyuan.bcywebview.biz.main.daily.DailyAllFragment;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;


public class DiscoverWorkFragment extends BaseFragment {
    public static final int TYPE_PRODUCTION = 0;
    public static final int TYPE_ALL = 1;

    private List<Fragment> mList = new ArrayList<>();
    private ViewPager mPager;
    private MyAdapter mAdapter;
    private TabLayout mTabLayout;

    private FragmentManager fm;

    private CircleProductionFragment mCircleProductionFragment;
    private DailyAllFragment mDailyAllFragment;
    private int tabItem = TYPE_PRODUCTION;
    private String[] tabs;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.discover_tag_layout, container, false);
        initArgs();
        getFragment();
        initUi(rootView);
        initAction();
        return rootView;
    }

    @Override
    protected void initArgs() {
        tabs = new String[]{getString(R.string.hot_acg_work), getString(R.string.all_circles)};
        tabItem = getArguments().getInt(gotoUtil.INTENT_VALUE_ONE, TYPE_PRODUCTION);
    }


    @Override
    protected void initUi(View rootview) {
        mAdapter = new MyAdapter(fm);
        mPager = (ViewPager) rootview.findViewById(R.id.pager);
        mTabLayout = (TabLayout) rootview.findViewById(R.id.tablayout);
        mPager.setOffscreenPageLimit(2);
        mPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mPager);
        if (tabItem == TYPE_ALL)
            mPager.setCurrentItem(1);
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                mTabLayout.getTabAt(0).setText(tabs[0]);
                mTabLayout.getTabAt(1).setText(tabs[1]);
            }
        });
    }

    private void getFragment() {
        fm = getChildFragmentManager();
        initProduction();
        initAll();
    }

    private void initProduction() {
        mCircleProductionFragment = new CircleProductionFragment();
        mList.add(mCircleProductionFragment);
    }

    private void initAll() {
        Bundle bundle = new Bundle();
        mDailyAllFragment = new DailyAllFragment();
        bundle.putInt(gotoUtil.INTENT_VALUE_ONE, DailyAllFragment.TYPE_CIRCLE);
        mDailyAllFragment.setArguments(bundle);
        mList.add(mDailyAllFragment);
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }
}
